<?php

namespace app\service;

use app\model\UnitInfo;

class CustomManage
{
    const AREA_LEVEL = [
        1 => 'province',
        2 => 'city',
        3 => 'area',
    ];

    public function spliceWhere($params = [])
    {
        $unitInfoModel = new UnitInfo();
        $unit_ids = [];
        //type类型
        if (isset($params['type']) && $params['type']) {
            $list = $unitInfoModel->where('type', $params['type'])->field('id')->select()->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }
        //企业名称
        if (isset($params['title']) && $params['title']) {
            $list = $unitInfoModel->where('title', 'like', '%' . $params['title'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }

        //联系人
        if (isset($params['contacts']) && $params['contacts']) {
            $list = $unitInfoModel->where('contacts', 'like', '%' . $params['contacts'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }
        //电话
        if (isset($params['mobile']) && $params['mobile']) {
            $list = $unitInfoModel->where('mobile', $params['mobile'])->field('id')->select()->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }

        //区域
        if (isset($params['area']) && $params['area']) {
            $list = $unitInfoModel->where(self::AREA_LEVEL[$params['area_level']], $params['area'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }
        //成立时间
        if ((isset($params['establish_start_date']) && $params['establish_start_date']) && (isset($params['establish_end_date']) && $params['establish_end_date'])) {
            $start_time = $params['establish_start_date'] . ' 00:00:00';
            $end_time = $params['establish_end_date'] . ' 23:59:59';
            $list = $unitInfoModel->where('establish_date', 'between', [$start_time, $end_time])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }
        //资质类别
        if (isset($params['qualifications_type']) && $params['qualifications_type']) {
            $list = $unitInfoModel->where('qualifications_type', 'like', '%' . $params['qualifications_type'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }
        //安全时间
        if ((isset($params['secure_expire_start_date']) && $params['secure_expire_start_date']) && (isset($params['secure_expire_end_date']) && $params['secure_expire_end_date'])) {
            $start_time = $params['secure_expire_start_date'] . ' 00:00:00';
            $end_time = $params['secure_expire_end_date'] . ' 23:59:59';
            $list = $unitInfoModel->where('secure_expire_date', 'between', [$start_time, $end_time])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $unit_ids = $this->handlePersonIds($unit_ids, $ids);
        }
        return $unit_ids;

    }

    public function handlePersonIds($person_ids, $other_ids)
    {
        if ($person_ids && $other_ids) {
            $person_ids = array_intersect($person_ids, $other_ids);
        } elseif ($person_ids && !$other_ids) {
            $person_ids = [];
        } elseif (!$person_ids && $other_ids) {
            $person_ids = $other_ids;
        } elseif (!$person_ids && !$other_ids) {
            $person_ids = [];
        }
        return $person_ids;
    }
}